'use strict';
const { Controller } = require('egg');
const Result = require('../util/result');

class UserController extends Controller {
  async signup() {
    const { ctx } = this;
    const body = ctx.request.body;
    if (!body.phone || !body.password) {
      ctx.body = Result.warn(-1, '参数不全');
      return;
    }
    const info = await ctx.service.user.create(body);
    ctx.body = info;
  }

  async signin() {
    const { ctx } = this;
    const body = ctx.request.body;
    if (!body.phone || !body.password) {
      ctx.body = Result.warn(-1, '参数不全');
      return;
    }
    const info = await ctx.service.user.login(body);
    ctx.body = info;
  }

  async updateInfo() {
    const body = this.ctx.request.body;
    const info = await this.ctx.service.user.update(body);
    this.ctx.body = info;
  }

  async changePass() {
    const body = this.ctx.request.body;
    if (!body.phone || !body.password) {
      this.ctx.body = Result.warn(-1, '参数不全');
      return;
    }
    const info = await this.ctx.service.user.changePass(body);
    this.ctx.body = info;
  }

  async validatePass() {
    const body = this.ctx.request.body;
    if (!body.phone || !body.password) {
      this.ctx.body = Result.warn(-1, '参数不全');
      return;
    }
    const info = await this.ctx.service.user.validatePass(body);
    this.ctx.body = info;
  }
}

module.exports = UserController;
